<?php
namespace app\Logic\Layui\ServiceProviders;

use app\Logic\Container;
use app\Logic\Logic;
use App\Model\AuthModel;
use App\Model\MenuModel;
use Illuminate\Http\Request;

class AuthProvider extends Logic implements ServiceProviderInterface
{
    public $app;

    public function __construct(\Closure $app)
    {
        $this->app = $app($this);
    }


    /**
     * @param Container $app
     */
    public static function register(Container $app)
    {
        !isset($app->values['Auth']) && $app->values['Auth'] = function (\Closure $app){
            return new self($app);
        };
    }


    public function lists()
    {
        $data = $this->app->input();

        $condition = [];

        if(isset($data['name'])){
            $condition[] = ['name', 'like', "%{$data['name']}%"];
        }
        $condition[] = ['pid' , 0];

        $authModel = new AuthModel();

        $start = start($data['page'], $data['limit']);
        $res = $authModel->where($condition)->offset($start)->limit($data['limit'])
            ->orderBy('sort', 'desc')->get();
        $count = $authModel->where($condition)->count();

        foreach ($res as $key => $item){
            $res[$key]['allChildAuth'] = $authModel->where(['pid' => $item['id']])
                ->orderBy('sort', 'desc')->get();
        }
        return ['code' => 0, 'count' => $count, 'data' => $res, 'msg' => ''];
    }


    public function create()
    {
        $data = $this->app->input();

        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');

        $menuModel = new AuthModel();

        $rules = $menuModel->getRules($data);
        $error = $menuModel->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }

        $menuModel->insert($data);

    }


    public function edit($id)
    {
        $data = $this->app->input();

        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        if($id == $data['pid']){
            throw new \Exception('不能选择本身!');
        }

        $data['updated_at'] = date('Y-m-d H:i:s');

        $menuModel = new AuthModel();

        $rules = $menuModel->getRules($data);

        $error = $menuModel->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }

        $menuModel->where(['id'=>$id])->update($data);
    }


    public function first($id)
    {
        $model = new AuthModel();

        return $model->where(['id'=>$id])->first()->toArray();
    }


    public function delete()
    {
        $ids = $this->app->input('id');

        return AuthModel::destroy($ids);
    }


    public function normAuth()
    {
        $authModel = new AuthModel();
        $auths = $authModel->get();

        return normSortArr($auths);
    }


    public function classificationAuthArr()
    {
        $model = new AuthModel();
        $auths = $model->where(['pid'=>0])->orderBy('sort')->get();

        foreach ($auths as $key => $auth){
            $auths[$key]['child'] = $model->where(['pid'=>$auth['id']])->orderBy('sort')->get();
        }

        return $auths;
    }


    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name,$arguments)
    {
        return $this->app->$name(...$arguments);
    }
}